#ifndef _COMENV_H_ 
#define _COMENV_H_

#pragma once
#include "stdafx.h"

#include <data/iparser.h>

class ComEnv :		  public IComEnv
					, public CUnknownImp 
{
public:
	ComEnv(void);
	~ComEnv(void);
	UNKNOWN_IMP2_(IComEnv, IObjectRun);

	std_method(init_class)(IBase* prot, IBase* punkOuter);

	//IObjectRun
	std_method(Init)(HINSTANCE hInstance, LPCTSTR lpCfgName);
	std_method_(INT, Run)();
	std_method(Uninit)();

	std_method(InitEnv)(HINSTANCE hInstance, LPCTSTR lpCfgFile);
	std_method_(INT, RunEnv)(const CLSID& mainPlugin);
	std_method(GetRot)(IBase** pRot, IID iid = _uuidof(IComRunningObjectTable));	
	std_method(GetObjectLoader)(IBase** pRot, IID iid = _uuidof(IObjectLoader));

private:
	HINSTANCE							m_hInstance;
	tstring								m_strCfgFile;
	tstring								m_strRunPath;
	tstring								m_strProcessName;

private:
	_lComPtr<IOLManager>				m_pOlManager;
	_lComPtr<IComRunningObjectTable>	m_pRot;
	_lComPtr<IBase>						m_pMainPlugin;
	
	CLSID								m_clsidMain;

private:
	struct  clsidNode
	{
		CLSID			clisd;
		unsigned long	ulStart;
		char			szParam[PARAMBUFSIZE+1];
		int				iUiPulgin;
		char			szName[NAMEBUFSIZE+1];
		clsidNode()
		{
			clisd		=	GUID_NULL;
			ulStart		=	-1;
			memset(szParam,0,PARAMBUFSIZE+1);
			memset(szName,0,NAMEBUFSIZE+1);
			iUiPulgin	=	0;
		}
		clsidNode(CLSID	csd,unsigned long ulindex,const char *pszParam,int iUi,const char* pszName)
		{
			if (pszName != NULL)
			{
				string_strcpy(szParam,pszParam);
			}
			if (pszName != NULL)
			{
				string_strcpy(szName,pszName);
			}
			clisd		=	csd;
			ulStart		=	ulindex;
			iUiPulgin	=	iUi;

		}

	};
	list<clsidNode>		m_listClsid;


	HRESULT	loadOlAndRot();
	HRESULT	CreatePluginObject(CLSID csPulgin,IBase** pModuleBase);
	HRESULT	loadConfigModule();


	tstring ModuleToPath(const TCHAR* pCompoents_path);
	HRESULT	RegisterModule(const TCHAR* pCompoents_path);

	HRESULT	InsertCacheList(char* pClsid,
									char* pName,
									char* pParam,
									char* pUi,
									char* pStart);
	HRESULT	SubMitPulgin();



	bool	getModuleInfo(HINSTANCE hInstance);
	bool	initEnvConfig(LPCTSTR lpCfgName);


	////////////////////////////////////////////////////////////////////////////////////////////

	HRESULT	LoadComConfig(IParser* pAnalysis);
	HRESULT	LoadBasecom(JsRoot* pRoot, JsNode pCommodule);
	HRESULT	LoadMaincom(JsRoot* pRoot, JsNode pCommodule);

	HRESULT	LoadComNode(JsRoot* pRoot, JsNode pCommodule,
					char**	pComponents_path,
					int*	pClsidinclude);

	HRESULT	LoadMainPulgin(JsRoot* pRoot, JsNode pCommodule);



	HRESULT	LoadComClsidNodeBase(JsRoot* pRoot, JsNode pCommodule,
					char** pClsid,
					char** pName,
					char** pParam,
					char** pUi,
					char** pStart);



};
#endif
